package data.utility;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Random;

import javax.imageio.ImageIO;

import po.AdvertisePO;
import po.ValidatePO;
import service.UtilityDataService;

public class UtilityData  extends UnicastRemoteObject implements UtilityDataService{
	private static final long serialVersionUID = 1L;
	private final String agreement="Agreement";
	private final Random random=new Random();
	private final String codeList="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	private Font[] fonts;
	private final float FONT_SIZE=30;
	
	private ArrayList<AdvertisePO> advertiseList;
	
	public UtilityData() throws RemoteException, FileNotFoundException {
		fonts=GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts();
		initAdverData();
		
	}
	
	private void initAdverData() throws FileNotFoundException{
		advertiseList=new ArrayList<AdvertisePO>();
		
		File file=new File("image/ad");
		if(!file.exists()){
			throw new FileNotFoundException();
		}
		
		for (File i:file.listFiles()){

			try {
				BufferedImage image=ImageIO.read(i);
				advertiseList.add(new AdvertisePO(getImageData(image)));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public String getAgreement()  throws RemoteException{
		return agreement;
	}

	@Override
	public ValidatePO getRamdomValidate() throws RemoteException{
		String validateCode=creatValidateCode();
		BufferedImage validateImage=createValidateImage(validateCode);
		
		return new ValidatePO(getImageData(validateImage), validateCode);
	}
	
	@Override
	public ArrayList<AdvertisePO> getAdvertisements() throws RemoteException {
		
		return advertiseList;
	}

	private byte[] getImageData(BufferedImage image){
		byte[] imageData = null;
		 
		if (image == null ) {
			return null;
		}
		 
		try {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write(image, "jpg", baos);
		imageData = baos.toByteArray();
		} catch (IOException ex) {
		  return null;
		}
		 
		return imageData;
	}

	private BufferedImage createValidateImage(String validateCode){
		BufferedImage image=new BufferedImage(ValidatePO.VALIDATEIMAGE_WIDTH, 
				ValidatePO.VALIDATEIMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB);
		Graphics graphics=image.getGraphics();
		
		
		int fontSelect=random.nextInt(fonts.length);
		Font randomFont=fonts[fontSelect].deriveFont(FONT_SIZE);
		graphics.setFont(randomFont);
		graphics.setColor(Color.GRAY);
		graphics.fillRect(0, 0, ValidatePO.VALIDATEIMAGE_WIDTH, 
				ValidatePO.VALIDATEIMAGE_HEIGHT);
		graphics.setColor(Color.WHITE);		
		
		for (int i = 0; i < 50; i++) {
			int r=random.nextInt(256);
			int g=random.nextInt(256);
			int b=random.nextInt(256);
			int x1=random.nextInt(100);
			int y1=random.nextInt(50);
			int x2=random.nextInt(100);
			int y2=random.nextInt(50);
			
			graphics.setColor(new Color(r,g,b));
			graphics.drawLine(x1, y1, x2, y2);
		}
		graphics.setColor(Color.WHITE);
		graphics.drawString(validateCode, 20, 30);
		
		return image;
	}
	
	private String creatValidateCode(){
		String randomCode="";
		for (int i = 0; i < 4; i++) {
			int pos=random.nextInt(codeList.length());
			randomCode=randomCode.concat(codeList.charAt(pos)+"");
		}
		
		return randomCode;
	}
}